package com.baidunewmusic;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidunewmusic.Playlist.Song;

public class MenuIconAdapter extends BaseAdapter {

	protected LayoutInflater mInflater;
	protected ArrayList<Song> _arrayList;
	
	private Context mContext;
	private Boolean bFirst = true;
	
	public MenuIconAdapter(Context context, ArrayList<Song> list) {
		mInflater = LayoutInflater.from(context);
		_arrayList = list;
		mContext = context;
		
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return _arrayList.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return _arrayList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;

		// When convertView is not null, we can reuse it directly, there is no
		// need
		// to reinflate it. We only inflate a new View when the convertView
		// supplied
		// by ListView is null.
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.song_list, null);
			

			// Creates a ViewHolder and store references to the two children
			// views
			// we want to bind data to.
			holder = new ViewHolder(convertView);

			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
		}

		// Bind the data efficiently with the holder.
//		if (position == 0 && bFirst) {
//			holder.getTitle().setTextColor(Color.GREEN);
//			bFirst = false;
//		}
//			else {
			holder.getTitle().setTextColor(new ColorStateList( 
					new int[][] { 
						new int[] { android.R.attr.state_selected}, 
						new int[0], 
					}, 
					//new int[] {Color.rgb(255, 128, 192), Color.BLUE, }
					new int[] {Color.GREEN, Color.WHITE}
			));
		//}
		
		holder.setData(_arrayList.get(position));
		//holder.getTitle().setText(_arrayList.get(position).title);
		
		ImageLoader.getInstance().load(holder.getIcon(), _arrayList.get(position).cover, true);

		//holder.getIcon().setScaleType(ImageView.ScaleType.FIT_CENTER); 
		
		
		convertView.setId(position);
		
		
		return convertView;
	}

	public class ViewHolder {
		View base;
		TextView title;
		ImageView icon;
		
		ColorStateList csl = new ColorStateList( 
					new int[][] { 
						new int[] { android.R.attr.state_selected}, 
						new int[0], 
					}, 
					//new int[] {Color.rgb(255, 128, 192), Color.BLUE, }
					new int[] {Color.WHITE, Color.rgb(200, 200, 192)}
		);
		
		ViewHolder(View v) {
			this.base = v;
		}
		
		public void setData(Song s){
			getTitle().setText(s.title);
			((TextView)base.findViewById(R.id.album)).setText("In: " + s.album);
			((TextView)base.findViewById(R.id.album)).setTextColor(csl);
			((TextView)base.findViewById(R.id.artist)).setText("By: " + s.artist);
			((TextView)base.findViewById(R.id.artist)).setTextColor(csl);
			((TextView)base.findViewById(R.id.date)).setText("Time: " + s.publishDate);
			((TextView)base.findViewById(R.id.date)).setTextColor(csl);
			
		}
		
		public TextView getTitle() {
			if ( title == null ) {
				title = (TextView)base.findViewById(R.id.title);
			}
			return title;
		}
		public void setTitle(TextView text) {
			this.title = text;
			
		}
		public ImageView getIcon() {
			if (icon == null){
				 icon = (ImageView) base.findViewById(R.id.cover);
			}
			return icon;
		}
		public void setIcon(ImageView icon) {
			this.icon = icon;
		}
		
		
		
	}

	
}
